package com.sram.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.ServletActionContext;
import com.sram.entity.User;
import com.sram.util.DateUtil;

import sun.misc.BASE64Encoder;
     /**导出excel 文件
	 * 导入3个jar包
	 * poi-3.10-FINAL-20140208.jar、、、poi-ooxml-3.9-20121203.jar、、、poi-ooxml-schemas-3.9-20121203.jar
	 * 必须有getIndexExportOrderExcelSj和getExcelFileName
	 * Struts
	 * <action name="exportByHbx" class="exportAction"  >
	 *	     <result type="stream">
	 *		<param name="contentType">application/vnd.ms-excel</param>
	 *			<param name="inputName">indexExportOrderExcelSj</param>
	 *			<param name="contentDisposition">attachment; filename="${excelFileName}"</param>
	 *</result>
	 *</action>
	 * 
	 * 
	 * 
	 * 
	 */
public class ExportAction extends BaseAction {
	
   public ByteArrayInputStream getIndexExportOrderExcelSj() throws IOException{
		List<User> user=userService.findall();
	 
	//每页显示的数量
	// 将文件的内容 缓存到 内存中，再从内存读入到输入流 ByteArrayOutputStream
			ByteArrayOutputStream byteArrayOutputStream = null;
			// 导出Excel
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFFont f = (HSSFFont) workbook.createFont(); // 设置字体内容
			HSSFSheet sheet = workbook.createSheet("所有的用户");
			Row row = sheet.createRow((int) 0);
			HSSFCellStyle cellStyle = workbook.createCellStyle();
			f.setFontHeightInPoints((short) 22);// 字号
			f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// 加粗
			cellStyle.setFont(f);
			cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 创建一个居中格式
			Cell cell = row.createCell((short) 0);
			cell.setCellValue("用户名");
			cell = row.createCell((short) 1);
			cell.setCellValue("密码");
			for (int i = 0; i < user.size(); i++) {
				row = sheet.createRow((int) i + 1);
				User userd = user.get(i);
				row.createCell(0).setCellValue(userd.getUserName());
				row.createCell(1).setCellValue(userd.getPassword());
			}
			byteArrayOutputStream = new ByteArrayOutputStream();
			workbook.write(byteArrayOutputStream);
			byteArrayOutputStream.close();
			return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
      }
   /**
	 * 下载导入表样
	 * @return
	 */
	private String filenameDownExcelOrder = "zancundanbiaoyang.xlsx";
	public String downExcel(){
		return "downExcelSUCCESS";
	}
	/**
	 * 下载导入订单表样
	 * @return
	 * @throws FileNotFoundException
	 */
	public InputStream getDownExcelStream() throws FileNotFoundException {
		String srcFile = ServletActionContext.getServletContext().getRealPath("/apk") + "/" + filenameDownExcelOrder;
		File file = new File(srcFile);// 得到一个file对象
		if (!file.exists()) {
			return null;
		}else{
			return new FileInputStream(file);// 返回一个文件输入流
		}
	}
	/**
	 * 在hbx导入订单页面，下载导入表样 提供编码后中文名
	 * @return
	 * @throws IOException
	 */
	public String getDownExcelOrderName() throws IOException {
		return encodeDownloadFilename("暂存单导入表样.xlsx",getRequest().getHeader("user-agent"));
	}
	
	

	// 提供编码后中文名
	public String getExcelFileName() throws IOException {
		return encodeDownloadFilename("导出Excel" + DateUtil.DateToString(new Date(), "yyyyMMddHHmmss") + ".xls",
				getRequest().getHeader("user-agent"));
	}

	/**
	 * 下载文件时，针对不同浏览器，进行附件名的编码
	 * 
	 * @param filename
	 *            下载文件名
	 * @param agent
	 *            客户端浏览器
	 * @return 编码后的下载附件名
	 * @throws IOException
	 */
	public String encodeDownloadFilename(String filename, String agent) throws IOException {
		if (agent.contains("Firefox")) { // 火狐浏览器
			filename = "=?UTF-8?B?" + new BASE64Encoder().encode(filename.getBytes("utf-8")) + "?=";
			filename = filename.replaceAll("\r\n", "");
		} else { // IE及其他浏览器
			filename = URLEncoder.encode(filename, "utf-8");
		}
		return filename;
	}

}